<!DOCTYPE html>
<html>
<head>
    <title>添加分类</title>
    {% include 'admin/common/header.html' %}
</head>
<body>
<form class="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">

                <div class="layui-form-item">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="classifyname" lay-verify="title" autocomplete="off" placeholder="请输入标题"
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">已有分类</label>
                    <div class="layui-input-block">
                    {% if classify %}
                        {% for item in classify %}
{#                            <span class="layui-badge-rim"></span>#}
                            <button class="pear-btn" id="btn" value="{{ item.classifyid }}" lay-filter="remove" onclick="cl({{ item.classifyid }},this)" >{{ item.gname }}</button>
                        {% endfor %}
                    {% else %}
                        <button class="pear-btn pear-btn-md" lay-event="#">暂无分类请添加</button>
                    {% endif %}


                    </div>
                </div>

                {#                <div class="layui-form-item">#}
                {#                    <label class="layui-form-label">角色</label>#}
                {#                    <div class="layui-input-block">#}
                {##}
                {#                        {% for role in roles %}#}
                {#                            <input#}
                {#                                    value="{{ role.id }}" title="{{ role.name }}" type="checkbox"#}
                {#                                    name="roleIds" lay-skin="primary">#}
                {#                        {% endfor %}#}
                {##}
                {#                    </div>#}
                {#                </div>#}
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
{% include 'admin/common/footer.html' %}
<script>
    layui.use(['upload','form', 'jquery'], function () {
        let form = layui.form
        let $ = layui.jquery
        let upload = layui.upload
        let imgpath = ''


        //拖拽上传
        upload.render({
            elem: '#uptest'
            , url: '{{ url_for('wxuser.upload_api') }}' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
            {#,url: 'https://httpbin.org/post'#}
            , exts: 'jpg|png|gif|bmp|jpeg'
            , done: function (res) {
                layer.msg('上传成功', {icon: 1});
                {#console.log(res.img)#}
                let html=" <img src='"+res.data.src+"' alt='上传成功后渲染' style='max-width: 103px;position: relative;margin-left: 190px;margin-top: -143px;display: inline-block'>"
                layui.$('#uploadDemoView').append(html)
                {#find('img').attr('src', res.data.src);#}
                imgpath = res.data.src
                console.log(imgpath)
                {#layui.$('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', res.files.file);#}
                console.log(res)
            }
        });


        form.on('submit(user-save)', function (data) {
            {#let roleIds = ''#}
            {#$('input[type=checkbox]:checked').each(function () {#}
            {#    roleIds += $(this).val() + ','#}
            {#//})#}
            {#roleIds = roleIds.substr(0, roleIds.length - 1)#}
            {#data.field.roleIds = roleIds#}
            {#data.field.file=imgpath#}
            $.ajax({
                url: '/goods/addclassify',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name))//关闭当前页
                            parent.layui.table.reload('user-table')
                        })
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000})
                    }
                }
            })
            return false
        })



        {#$(document).on('click','#btn',function(obj){#}
        {#        var Nick = $('#user').val();#}
        {#//});#}
        window.cl=function (classifyid,obj){
            console.log(classifyid)
            console.log(obj)
            {#parent.layer.open({#}
            {#    type:1,#}
            {#    title:'提了啥',#}
                {#area: ['680px', '500px'],#}
            {#    content: '提了啥',//支持获取DOM元素#}
            {#    btn:['确定', '取消'],#}
            {#    scrollbar:false,#}
            {#    yes: function(index){//layer.msg('yes');    //点击确定回调#}
            {#        layer.close(index);#}
            {#        showToast();#}
            {#    },#}
            {##}
            {#//});#}
            parent.layer.confirm('确定要删除该分类吗？', { icon: 3, title: '提示' }, function (index) {
            layer.close(index)
            {#let loading = layer.load()#}
            $.ajax({
              url: '/goods/removeclassify?id='+classifyid,
              dataType: 'json',
              type: 'DELETE',
              success: function (result) {
                  console.log(1111)
                {#layer.close(loading)#}
                if (result.success) {
                  {#popup.success(result.msg, function () {#}
                    {#obj.del()#}
                  {#//})#}
                } else {
                  {#popup.failure(result.msg)#}
                }
              }
            })
          })
        }



    })
</script>
<script>
</script>
</body>
</html>